import { Directive, HostListener } from '@angular/core';
import { FootmarkTrackService, ProcessURLResult } from '../services/footmark-track.service';
import { Router } from '@angular/router';
@Directive({
  selector: 'a.historyBack'
})
export class HistoryBackDirective {
  private currentURL: string;

  constructor(private router: Router, private footMark: FootmarkTrackService) {
    this.currentURL = router.url;
  }

  @HostListener('click', ['$event.target'])
  public backHistory($event: Event): void {
    let previousURL: string | null = this.footMark.getPreviousRef(this.currentURL);
    //console.log('[HisBack-DIR]current url: '+this.currentURL+', back url: '+previousURL);
    let data: ProcessURLResult = this.footMark.processURL(previousURL || '/home');
    //console.dir(data);
    this.router.navigate([data.path], { queryParams: data.queryParams });
  }
}
